package com.njupt.HashTable;

import java.util.ArrayList;
import java.util.List;

/**
 *

 438. 找到字符串中所有字母异位词
 */
public class FindAnagrams {


    public boolean isAnagrams(String s1,String s2){
        if(s1.length()!=s2.length()){
            return false;
        }

        int len = s1.length();
        int nums[] = new int[26];

        for (int i = 0; i < len; i++) {
            nums[s1.charAt(i)-'a']++;
            nums[s2.charAt(i)-'a']--;
        }

        for (int num:nums){
            if(num!=0){
                return false;
            }
        }
        return true;
    }

    public List<Integer> findAnagrams(String s, String p) {
        List<Integer> integers = new ArrayList<>();

        int pLen = p.length();
        int startIndex = 0;
        int endIndex = startIndex+pLen;

        for (int i = 0; i <= s.length()-pLen; i++) {
            startIndex = i;
            endIndex = startIndex + pLen;
            if(isAnagrams(s.substring(startIndex,endIndex),p)){
                integers.add(startIndex);
            }
        }

        return integers;

    }
    public static void main(String[] args) {
        String s = "cbaebabacd";
        String p = "abc";
        FindAnagrams test = new FindAnagrams();
        System.out.println(test.findAnagrams(s, p));
    }
}
